package net.pinnoocle.playservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.pinnoocle.playcommon.utils.ConvertUtils;
import net.pinnoocle.playdao.dao.BannersDao;
import net.pinnoocle.playdao.entity.Banners;
import net.pinnoocle.playdao.entity.shopPc.banner.SaveBannerParams;
import net.pinnoocle.playdao.entity.web.banners.BannerListPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 横幅图(Banners)表服务实现类
 *
 * @author ye
 * @since 2021-11-13 11:29:39
 */
@Service("bannersService")
@Slf4j
public class BannersServiceImpl extends ServiceImpl<BannersDao, Banners> {

    @Autowired
    UserServiceImpl userService;

    /**
     * 横幅图
     * @return
     */
    public List<BannerListPageVo> bannerList(){
        String shopId = userService.getGlobalShopId();
        List<Banners> list = this.list(new QueryWrapper<Banners>()
                .lambda()
                .eq(Banners::getShopId, shopId));
        return ConvertUtils.sourceToTarget(list,BannerListPageVo.class);
    }

    /**
     * 添加Banner
     * @param params
     * @return
     */
    public boolean saveBanner(SaveBannerParams params){
        String shopId = userService.getGlobalShopId();
        Banners banners = ConvertUtils.sourceToTarget(params, Banners.class);
        banners.setShopId(shopId);
        banners.setCreatedTime(new Date());
        try {
            return this.save(banners);
        } catch (Exception e) {
            log.info("添加banner失败："+e.getMessage());
        }
        return false;
    }

    /**
     * 编辑Banner
     * @param params
     * @return
     */
    public boolean updateBanner(SaveBannerParams params){
        Banners banners = ConvertUtils.sourceToTarget(params, Banners.class);
        try {
            return this.updateById(banners);
        } catch (Exception e) {
            log.info("编辑banner失败："+e.getMessage());
        }
        return false;
    }

    /**
     * 删除banner
     * @param bannerId
     * @return
     */
    public boolean deleteBanner(String bannerId){
        try {
            return this.removeById(bannerId);
        } catch (Exception e) {
            log.info("banner删除失败："+e.getMessage());
            return false;
        }
    }
}